package main.java.DMDE.view;

import java.awt.Container;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import main.java.DMDE.infrastructure.visual.OperationNode;
import main.java.DMDE.model.CardinalityValues;
import main.java.DMDE.model.Entity;
import main.java.DMDE.model.Method;
import main.java.DMDE.model.relationships.RelationshipComponent;

import antlr.collections.impl.Vector;

import com.jgoodies.forms.factories.CC;

public class RelationshipEntityView {

    private JLabel relationNameText;
    private JComboBox<String> relationComboBox;
    private JLabel maxCardText;
    private JLabel minCardText;
    private JComboBox<String> maxCard;
    private JComboBox<String> minCard;
    private String maxValues[] = {CardinalityValues.ONE.toString(),CardinalityValues.MANY.toString()};
    private String minValues[] = {CardinalityValues.CERO.toString(),CardinalityValues.ONE.toString()};
    
    public RelationshipEntityView() {

    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void addLabels(Container frameContentPane, int col, int row) {

	relationNameText = new JLabel();
	relationComboBox = new JComboBox<String>();
	relationNameText.setText("Entity name: ");
	relationNameText.setLabelFor(relationComboBox);
	frameContentPane.add(relationNameText, CC.xy(col, row));
	frameContentPane.add(relationComboBox, CC.xy(col + 2, row));
	
	maxCardText = new JLabel();
	minCardText = new JLabel();
	maxCardText.setText("Max Cardinality");
	minCardText.setText("Min Cardinality");
	
	
	maxCard = new JComboBox(maxValues);
	minCard = new JComboBox(minValues);
	maxCardText.setLabelFor(maxCard);
	minCardText.setLabelFor(minCard);
	
	frameContentPane.add(maxCardText, CC.xy(col, row + 4));
	frameContentPane.add(minCardText, CC.xy(col, row + 2));
	frameContentPane.add(maxCard, CC.xy(col + 2, row + 4));
	frameContentPane.add(minCard, CC.xy(col + 2, row + 2));
    }

    public void initialize() {
	relationComboBox.setEnabled(true);
	maxCard.setEnabled(true);
	minCard.setEnabled(true);
	relationComboBox.removeAllItems();
	relationComboBox.repaint();
	relationComboBox.updateUI();
	minCard.repaint();
	maxCard.updateUI();
    }
    
    public void setDefault(){
	relationComboBox.setEnabled(false);
	maxCard.setEnabled(false);
	minCard.setEnabled(false);
    }

    public void setEntities(List<String> entities) {
	initialize();
	for(String entity: entities)
	    relationComboBox.addItem(entity);
    }

    public void addActionListener(ActionListener actionListener) {
	relationComboBox.addActionListener(actionListener);
	
    }

    public String getSelectedEntity() {
	return (String) relationComboBox.getSelectedItem();
    }

    public CardinalityValues getMax() {
	String max = (String) maxCard.getSelectedItem();
	for(CardinalityValues cardinality : CardinalityValues.values()){
	    if(cardinality.getDesc().equals(max))
		return cardinality;
	}
	return null;
    }

    public CardinalityValues getMin() {
	String min = (String) minCard.getSelectedItem();
	for(CardinalityValues cardinality : CardinalityValues.values()){
	    if(cardinality.getDesc().equals(min))
		return cardinality;
	}
	return null;
    }

    public void selectValue(RelationshipComponent component) {
	relationComboBox.setSelectedItem(component.getEntity().toString());
	maxCard.setSelectedItem(component.getMaxCardinality().toString());
	minCard.setSelectedItem(component.getMinCardinality().toString());
    }

    public void setEntity(Entity entity) {
	
    }

    public void diableCardinality() {
	this.maxCard.setEnabled(false);
	this.minCard.setEnabled(false);
    }

}